package cn.iocoder.yudao.module.social.service.interaction;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.social.controller.admin.interaction.vo.InteractionCreateReqVO;
import cn.iocoder.yudao.module.social.controller.admin.interaction.vo.InteractionPageReqVO;
import cn.iocoder.yudao.module.social.convert.interaction.InteractionConvert;
import cn.iocoder.yudao.module.social.dal.dataobject.interaction.InteractionDO;
import cn.iocoder.yudao.module.social.dal.mysql.interaction.InteractionMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import jakarta.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.social.enums.ErrorCodeConstants.INTERACTION_NOT_EXISTS;

/**
 * 互动 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
@Slf4j
public class InteractionServiceImpl implements InteractionService {

    @Resource
    private InteractionMapper interactionMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createInteraction(InteractionCreateReqVO createReqVO) {
        // 插入
        InteractionDO interaction = InteractionConvert.INSTANCE.convert(createReqVO);
        interactionMapper.insert(interaction);
        // 返回
        return interaction.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteInteraction(Long id) {
        // 校验存在
        validateInteractionExists(id);
        // 删除
        interactionMapper.deleteById(id);
    }

    private void validateInteractionExists(Long id) {
        if (interactionMapper.selectById(id) == null) {
            throw exception(INTERACTION_NOT_EXISTS);
        }
    }

    @Override
    public InteractionDO getInteraction(Long id) {
        return interactionMapper.selectById(id);
    }

    @Override
    public List<InteractionDO> getInteractionList(Collection<Long> ids) {
        return interactionMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<InteractionDO> getInteractionPage(InteractionPageReqVO pageReqVO) {
        return interactionMapper.selectPage(pageReqVO);
    }

    @Override
    public List<InteractionDO> getInteractionListByContent(Long contentId) {
        return interactionMapper.selectListByContentId(contentId);
    }

    @Override
    public List<InteractionDO> getInteractionListByUser(Long userId) {
        return interactionMapper.selectListByUserId(userId);
    }

    @Override
    public List<InteractionDO> getInteractionListByType(String type) {
        return interactionMapper.selectListByType(type);
    }

    @Override
    public void validateInteractionList(Collection<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return;
        }
        
        List<InteractionDO> interactions = interactionMapper.selectBatchIds(ids);
        if (interactions.size() != ids.size()) {
            throw exception(INTERACTION_NOT_EXISTS);
        }
    }

}
